package OnlineShopping;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Admin extends User {
    public Admin(String userId, String username, String password, String email, String phone, String registrationDate, String userLevel, double totalSpent,HistoryRecord history) {
        super(userId, username, password, email, phone, registrationDate, userLevel, totalSpent,history);
    }
    public boolean login(String username, String password) {
        // 阅读 Admin.xlsx 文件并验证用户名和密码
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Admin.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                Cell usernameCell = row.getCell(1); // 假设用户名在第二列
                Cell passwordCell = row.getCell(2); // 假设密码在第三列

                if (usernameCell.getStringCellValue().equals(username) &&
                        passwordCell.getStringCellValue().equals(password)) {

                    return true; // 登录成功
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false; // 登录失败
    }
    //解除锁定
    public boolean unlockCustomerAccount(String userId) {
        boolean customerFound = false;
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);

            for (Row row : sheet) {
                Cell idCell = row.getCell(0); // 假设用户ID在第一列
                if (idCell != null && idCell.getStringCellValue().equals(userId)) {
                    // 找到客户后，重置其登录尝试和锁定状态
                    Cell lockedCell = row.getCell(8); // 假设锁定状态在第9列
                    if (lockedCell != null) {
                        lockedCell.setCellValue("否"); // 更新为未锁定状态
                        customerFound = true;
                        System.out.println("账户 " + userId + " 已解锁。");
                    }
                    break;
                }
            }
            if (customerFound) {
                try (FileOutputStream fos = new FileOutputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx")) {
                    workbook.write(fos);
                }
            } else {
                System.out.println("未找到该用户。");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return customerFound;
    }
    //重置客户密码
    public void resetCustomerPassword(String customerId, String newPassword) {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            boolean customerFound = false;

            for (Row row : sheet) {
                Cell idCell = row.getCell(0); // 假设用户ID在第一列
                String idValue = "";

                if (idCell != null) {
                    switch (idCell.getCellType()) {
                        case STRING:
                            idValue = idCell.getStringCellValue().trim(); // 去除空格
                            break;
                        case NUMERIC:
                            idValue = String.valueOf((int) idCell.getNumericCellValue()).trim(); // 转换为字符串并去除空格
                            break;
                        default:
                            break;
                    }
                }

                if (idValue.equals(customerId.trim())) { // 去除传入ID的空格
                    Cell passwordCell = row.getCell(7); // 假设密码在第八列
                    if (passwordCell != null) {
                        passwordCell.setCellValue(newPassword); // 更新客户密码
                        customerFound = true;
                    }
                    break; // 找到用户后可直接退出循环
                }
            }

            if (customerFound) {
                System.out.println("客户密码重置成功。");
            } else {
                System.out.println("未找到该客户。");
            }

            // 保存更改到文件
            try (FileOutputStream fos = new FileOutputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx")) {
                workbook.write(fos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //列出所有客户信息
    public void listCustomers() {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            System.out.println("客户信息：");
            for (Row row : sheet) {
                String userId = getCellValueAsString(row.getCell(0));
                String userName = getCellValueAsString(row.getCell(1));
                String email = getCellValueAsString(row.getCell(6));

                System.out.println("用户ID: " + userId +
                        ", 用户名: " + userName +
                        ", 邮箱: " + email);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //辅助
    private String getCellValueAsString(Cell cell) {
        if (cell == null) {
            return ""; // 或者返回 null，根据需求
        }

        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                return String.valueOf(cell.getNumericCellValue());
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula(); // 返回公式字符串
            case BLANK:
                return ""; // 空单元格返回空字符串
            default:
                return ""; // 处理其他类型（如错误类型）
        }
    }
    //删除客户信息
    public void deleteCustomer(String customerId) {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            boolean customerFound = false;
            for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                Cell idCell = row.getCell(0); // 假设用户ID在第一列

                if (idCell != null) {
                    String idValue = getCellValueAsString(idCell); // 使用辅助方法获取字符串值
                    idValue = idValue.trim(); // 去除空格

                    if (idCell.getCellType() == CellType.NUMERIC) {
                        // 如果 ID 是数字，转换为字符串进行比较
                        if (String.valueOf((int) idCell.getNumericCellValue()).equals(customerId.trim())) {
                            sheet.removeRow(row); // 删除该行
                            customerFound = true;
                            break;
                        }
                    } else {
                        // 如果 ID 是字符串，直接比较
                        if (idValue.equals(customerId.trim())) {
                            sheet.removeRow(row); // 删除该行
                            customerFound = true;
                            break;
                        }
                    }
                }
            }
            if (customerFound) {
                System.out.println("客户信息已成功删除。");
            } else {
                System.out.println("未找到该客户。");
            }
            try (FileOutputStream fos = new FileOutputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx")) {
                workbook.write(fos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //查询客户信息
    public void queryCustomer(String customerId) {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            boolean customerFound = false;

            for (Row row : sheet) {
                Cell idCell = row.getCell(0); // 假设用户ID在第一列
                String idValue = "";

                if (idCell != null) {
                    switch (idCell.getCellType()) {
                        case STRING:
                            idValue = idCell.getStringCellValue().trim(); // 去除空格
                            break;
                        case NUMERIC:
                            idValue = String.valueOf((int) idCell.getNumericCellValue()).trim(); // 转换为字符串并去除空格
                            break;
                        default:
                            // 如果单元格类型是其他类型，可以设置为默认值或处理
                            idValue = "";
                    }
                }

                if (idValue.equals(customerId.trim())) { // 去除传入ID的空格
                    System.out.println("用户ID: " + idValue +
                            ", 用户名: " + row.getCell(1).getStringCellValue() +
                            ", 邮箱: " + row.getCell(6).getStringCellValue());
                    customerFound = true;
                    break; // 找到用户后直接退出循环
                }
            }

            if (!customerFound) {
                System.out.println("未找到该客户。");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //列出商品信息
    public void listProducts() {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);

            System.out.println("商品信息：");
            for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) { // 从第 1 行开始
                Row row = sheet.getRow(rowIndex); // 获取当前行

                if (row != null) { // 确保当前行不是 null
                    // 读取商品ID
                    String productId = "";
                    Cell idCell = row.getCell(0);
                    if (idCell != null) {
                        if (idCell.getCellType() == CellType.STRING) {
                            productId = idCell.getStringCellValue();
                        } else if (idCell.getCellType() == CellType.NUMERIC) {
                            productId = String.valueOf(idCell.getNumericCellValue());
                        } else {
                            System.err.println("商品ID单元格类型不正确: " + idCell.getCellType());
                        }
                    }

                    // 读取商品名称
                    String productName = "";
                    Cell nameCell = row.getCell(1);
                    if (nameCell != null) {
                        if (nameCell.getCellType() == CellType.STRING) {
                            productName = nameCell.getStringCellValue();
                        } else if (nameCell.getCellType() == CellType.NUMERIC) {
                            productName = String.valueOf(nameCell.getNumericCellValue());
                        } else {
                            System.err.println("商品名称单元格类型不正确: " + nameCell.getCellType());
                        }
                    }

                    // 读取价格
                    double price = 0.0; // 初始化价格
                    Cell priceCell = row.getCell(5);
                    if (priceCell != null) {
                        if (priceCell.getCellType() == CellType.NUMERIC) {
                            price = priceCell.getNumericCellValue(); // 获取数值
                        } else if (priceCell.getCellType() == CellType.STRING) {
                            try {
                                price = Double.parseDouble(priceCell.getStringCellValue()); // 转换字符串为数字
                            } catch (NumberFormatException e) {
                                System.err.println("无法将价格字符串转换为数字: " + priceCell.getStringCellValue());
                            }
                        } else {
                            System.err.println("价格单元格类型不正确: " + priceCell.getCellType());
                        }
                    } else {
                        System.err.println("价格单元格是 null");
                    }

                    // 输出商品信息
                    System.out.println("商品ID: " + productId +
                            ", 商品名称: " + productName +
                            ", 价格: " + price);
                } else {
                    System.err.println("行 " + rowIndex + " 是 null");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //删除商品信息
    public void deleteProduct(String productId) {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            boolean productFound = false;

            for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                Cell idCell = row.getCell(0); // 假设商品ID在第一列
                String idValue = "";

                if (idCell != null) {
                    switch (idCell.getCellType()) {
                        case STRING:
                            idValue = idCell.getStringCellValue().trim(); // 获取字符串并去除空格
                            break;
                        case NUMERIC:
                            idValue = String.valueOf((int) idCell.getNumericCellValue()).trim(); // 转换为字符串并去除空格
                            break;
                        default:
                            idValue = ""; // 其他类型处理
                    }
                }

                if (idValue.equals(productId.trim())) { // 比较时去除空格
                    sheet.removeRow(row); // 删除该行
                    productFound = true;
                    break; // 找到后退出循环
                }
            }

            if (productFound) {
                System.out.println("商品信息已成功删除。");
            } else {
                System.out.println("未找到该商品。");
            }

            // 写入修改后的数据
            try (FileOutputStream fos = new FileOutputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx")) {
                workbook.write(fos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //查询商品信息
    public void queryProduct(String productId) {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            boolean productFound = false;

            for (Row row : sheet) {
                Cell idCell = row.getCell(0); // 假设商品ID在第一列
                String idValue = getCellValueAsString(idCell).trim(); // 使用辅助方法获取商品ID并去除空格

                // 打印读取的商品ID
                System.out.println("读取的商品ID: " + idValue);

                if (idValue.equals(productId.trim())) { // 比较时也去除空格
                    System.out.println("商品ID: " + idValue +
                            ", 商品名称: " + getCellValueAsString(row.getCell(1)) + // 获取商品名称
                            ", 价格: " + row.getCell(5).getNumericCellValue()); // 获取价格
                    productFound = true;
                    break;
                }
            }
            if (!productFound) {
                System.out.println("未找到该商品。");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //添加商品信息
    public void addProduct(String productId, String productName, double price) {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
            Row newRow = sheet.createRow(lastRowNum + 1);
            newRow.createCell(0).setCellValue(productId);
            newRow.createCell(1).setCellValue(productName);
            newRow.createCell(5).setCellValue(price);

            try (FileOutputStream fos = new FileOutputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx")) {
                workbook.write(fos);
            }
            System.out.println("商品添加成功。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //修改商品信息
    public void modifyProduct(String productId, String newProductName, double newPrice) {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            boolean productFound = false;

            for (Row row : sheet) {
                Cell idCell = row.getCell(0); // 假设商品ID在第一列
                String idValue = "";

                if (idCell != null) {
                    switch (idCell.getCellType()) {
                        case STRING:
                            idValue = idCell.getStringCellValue().trim(); // 获取并去除空格
                            break;
                        case NUMERIC:
                            idValue = String.valueOf((int) idCell.getNumericCellValue()).trim(); // 转换为字符串
                            break;
                        default:
                            idValue = ""; // 处理其他类型或设为默认值
                    }
                }

                if (idValue.equals(productId.trim())) { // 比较时去除空格
                    row.getCell(1).setCellValue(newProductName); // 更新商品名称
                    row.getCell(5).setCellValue(newPrice); // 更新价格
                    productFound = true;
                    break; // 找到后退出循环
                }
            }

            if (productFound) {
                System.out.println("商品信息已成功修改。");
            } else {
                System.out.println("未找到该商品。");
            }

            // 写入修改后的数据
            try (FileOutputStream fos = new FileOutputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx")) {
                workbook.write(fos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //退出登录
    public void logout() {
        // 退出登录逻辑
        System.out.println("管理员 " + getUsername() + " 已成功退出登录。");
    }

}

